import 'package:acgn_pioneer/plugin/ushare/lib/ushare.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../public.dart';

class ShareIcon {
  String iconName;
  String text;
  int platform;

  ShareIcon({this.iconName, this.text, this.platform});
}

/// 分享组件
class ShareComponent extends StatelessWidget {
  ShareComponent(
      {Key key, this.title, this.imageUrl, this.url, this.desc, this.popTitle})
      : super(key: key);

  /// 弹窗标题
  final String popTitle;

  /// 分享标题
  final String title;

  /// 分享描述
  final String desc;

  /// 分享图片
  final String imageUrl;

  /// 分享链接
  final String url;

  final List<ShareIcon> list = [
    ShareIcon(iconName: 'share_rd_qq', text: 'QQ', platform: 0),
    ShareIcon(iconName: 'share_rd_qqkj', text: 'QQ空间', platform: 1),
    ShareIcon(iconName: 'share_rd_wx', text: '微信', platform: 2),
    ShareIcon(iconName: 'share_rd_pyq', text: '朋友圈', platform: 3),
    ShareIcon(iconName: 'share_rd_wb', text: '微博', platform: 4),
    ShareIcon(iconName: 'share_rd_lj', text: '复制链接', platform: 5),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.only(top: 20),
        child: Column(
          children: [
            Text(popTitle, style: TextStyle(color: Colors.white, fontSize: 16)),
            SizedBox(height: 6),
            Text('分享到qq/微信群会有很多好友看到哦',
                style: TextStyle(
                    color: ColorUtil.rgba(255, 255, 255, 0.6), fontSize: 12)),
            Container(
                padding: EdgeInsets.only(bottom: 10),
                child: GridView.builder(
                    shrinkWrap: true,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 4,
                        childAspectRatio: 1,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10),
                    itemCount: list.length,
                    itemBuilder: (context, index) {
                      return buildShare(list[index], context);
                    })),
            GestureDetector(
                child: Container(
                  width: double.infinity,
                  padding: EdgeInsets.only(top: 10, bottom: 10),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      border: Border(
                          top: BorderSide(
                              color: ColorUtil.rgba(0, 0, 0, 1),
                              width: 1,
                              style: BorderStyle.solid))),
                  child: Text('取消', style: TextStyle(color: Colors.white)),
                ),
                onTap: () {
                  Navigator.pop(context);
                })
          ],
        ));
  }

  Widget buildShare(ShareIcon shareIcon, BuildContext context) {
    return GestureDetector(
      child: Column(children: [
        SVG.icon(shareIcon.iconName, color: Colors.yellow, size: 48),
        SizedBox(height: 4),
        Text(
          shareIcon.text,
          style: TextStyle(color: Colors.white),
        )
      ]),
      onTap: () {
        if (shareIcon.platform == 5) {
          Clipboard.setData(ClipboardData(text: url));
          ACGN.toast.message('复制成功');
        } else {
          Ushare.doShare(
              title: title,
              desc: desc,
              url: url,
              imageUrl: imageUrl,
              platform: shareIcon.platform);
        }
        Navigator.pop(context);
      },
    );
  }
}
